package cn.rongdao.jrkabao.ui.cashadvance.addcard;

import android.content.Context;
import android.os.Bundle;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import org.greenrobot.eventbus.EventBus;

import cn.rongdao.jrkabao.bean.addcard.CardInfo;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.cashadvance.CashAddCreditCardModel;
import cn.rongdao.jrkabao.ui.cashadvance.CashAdvanceActivity;
import cn.rongdao.jrkabao.ui.cashadvance.event.ChangeCreditCardEvent;
import cn.rongdao.jrkabao.ui.identify.ProgressValidateIdCardActivity;
import cn.rongdao.jrkabao.ui.main.MainActivity;

/**
 * @author jiangyongxing
 * @date 2017/6/2
 * 描述：
 */

public class AddCreditCardForCashActivity extends BaseAddCreditCardOrDepositCardActivity {

    private boolean isProgress;
    private boolean isBank;
    private boolean isFromMain;
    private String institutionId;

    public static void start(Context context, boolean isProgress, boolean isBank, String institutionId, boolean isFromMain) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(IS_PROGRESS, isProgress);
        bundle.putBoolean(IS_BANK, isBank);
        bundle.putString(CashAdvanceActivity.INSTITUTION_ID, institutionId);
        bundle.putBoolean(ProgressValidateIdCardActivity.IS_FROM_MAIN, isFromMain);
        FRStartActivity.start(context, AddCreditCardForCashActivity.class, bundle);
    }

    public static void start(Context context) {
        start(context, false, false, "", false);
    }

    @Override
    protected void initView() {
        isProgress = getIntent().getBooleanExtra(IS_PROGRESS, false);
        isBank = getIntent().getBooleanExtra(IS_BANK, false);
        isFromMain = getIntent().getBooleanExtra(ProgressValidateIdCardActivity.IS_FROM_MAIN, false);
        institutionId = getIntent().getStringExtra(CashAdvanceActivity.INSTITUTION_ID);
        super.initView();
    }

    @Override
    protected boolean isCreditCard() {
        return true;
    }

    @Override
    protected boolean isProgress() {
        return isProgress;
    }

    @Override
    protected void saveCard(String bankNumber, String phone, CardInfo cardInfo, String realName, String identityNum) {
        addCardMap.put("bank_number", FRString.valueOf(bankNumber));
        addCardMap.put("phone", FRString.valueOf(phone));
        addCardMap.put("bank_name", FRString.valueOf(cardInfo.getBankname()));
        addCardMap.put("bank_icon", FRString.valueOf(cardInfo.getImg()));
        addCardMap.put("card_type", FRString.valueOf(cardInfo.getCardtype()));
        addCardMap.put("obverse_url", FRString.valueOf(observerUrl));
        if (validate(bankNumber, phone, cardInfo)) return;
        setSubmitCanClick(false);
        CashAddCreditCardModel.getInstance().execute(addCardMap, new FRBaseObserver<String>(this, "正在保存，请稍候...") {
            @Override
            protected void onBaseNext(String data) {
                setSubmitCanClick(true);
                FRToast.showToastSafe("添加成功");
                EventBus.getDefault().post(new ChangeCreditCardEvent());
                if (isProgress) {
                    if (!isBank) {      //没绑过储蓄卡，要去绑储蓄卡界面
                        AddDepositCardActivity.start(thisActivity(), true, true, institutionId, isFromMain);
                    } else if (isFromMain) {
                        MainActivity.Companion.start(thisActivity(), InfoConstant.FRAGMENT_TAG_CASHADVANCE);
                    } else {
                        CashAdvanceActivity.startFromAddCard(thisActivity());
                    }
                }
                finish();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                showDialog(t.getMessage());
                setSubmitCanClick(true);
            }
        });
    }

    @Override
    protected boolean isFromMain() {
        return isFromMain;
    }

    @Override
    protected String getCurrentTitle() {
        return "添加信用卡";
    }
}